import pandas as pd
import numpy as np
arr = np.arange(1, 10).reshape(3, 3)
df1 = pd.DataFrame(arr, columns=['a', 'b', 'c'])
df1.insert(0, 'key', ['001', '003', '002'])
print('左对象原始数据df1:\n', df1)
arr2 = np.arange(10, 14).reshape(2, 2)
df2 = pd.DataFrame(arr2, columns=['a', 'e'])
df2.insert(0, 'key', ['001', '004'])
print('右对象原始数据df2:\n', df2)
df3 = pd.merge(df1 ,df2, how='left')
print('以具有相同标签的所有列左合并的数据df3:\n', df3)
df4 = pd.merge(df1 ,df2, how='left', on='key')
print('以key列左合并的数据df4:\n', df4)
df5 = pd.merge(df1 ,df2, how='right', on='key')
print('以key列右合并的数据df5:\n', df5)
df6 = pd.merge(df1, df2, how='inner', on='key', suffixes=('_l', '_r'))
print('以key列内合并，并设置附加后缀的数据df6:\n', df6)
df7 = pd.merge(df1, df2, how='outer', on='key', sort=True)
print('以key列外合并，并按连接列排序的数据df7:\n', df7)




import pandas as pd
import numpy as np
arr1 = np.arange(1, 10).reshape(3, 3)
df1 = pd.DataFrame(arr1, columns=['a', 'b', 'c'])
df1.insert(0, 'key', ['001', '003', '002'])
print('第一个对象原始数据df1:\n', df1)
arr2 = np.arange(10, 14).reshape(2, 2)
df2 = pd.DataFrame(arr2, columns=['a', 'e'])
df2.insert(0, 'key', ['001', '004'])
print('第二个对象原始数据df2:\n', df2)
df3 = pd.concat([df1, df2])
print('纵向外合并的数据df3:\n', df3)
df4 = pd.concat([df1, df2], keys=['df1', 'df2'], sort=True)
print('纵向外合并，并标记每个对象，并按列标签排序的数据df4:\n', df4)
df5 = pd.concat([df1, df2], join='inner', ignore_index=True)
print('纵向内合并并重新设置连续行标签的数据df5:\n', df5)


import pandas as pd
import numpy as np
arr1 = np.arange(1, 10).reshape(3, 3)
df1 = pd.DataFrame(arr1, columns=['a', 'b', 'c'])
print('原始数据：\n', df)
print('每列求和聚合：\n', df.agg('sum'))
print('每列同时求和及平均值聚合：\n', df.agg(['sum', 'mean']))
def range(arr):
    if __name__ == '__main__':
        return arr.max() - arr
    print('各行分别求和，平均值和极差聚合：\n', df.agg({0:'sum',1:'mean',2: rang}, axis=1))



